package org.xdq.springcloud.demo20191222.controller;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.xdq.base.Constants;
import org.xdq.base.CurrUser;
import org.xdq.dto.UserDto;
import org.xdq.exception.BusinessException;
import org.xdq.exception.SysException;
import org.xdq.springcloud.demo20191222.service.LoginService;
import org.xdq.base.Result;

@RestController
public class LoginController {
	
	private static final Logger LOG = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private LoginService loginService;

	@PostMapping("/user")
	public Result login(@RequestBody UserDto dto, HttpSession session) {
		
		try {
			
			CurrUser user = loginService.checkLogin(dto);
			session.setAttribute(Constants.SESSION_ATTR_CURR_USER, user);

			return Result.success("登录成功！");

		} catch (BusinessException e) {

			return Result.fail(e.getMessage());

		} catch (SysException e) {

			e.printStackTrace();
			return Result.fail("系统升级中......");

		}
	}
	
//	
//	@RequestMapping("/test")
//	public void empty() {
//		LOG.info(String.valueOf(System.currentTimeMillis()));
//	}
//	
	
}
